//
//  ZCSearchFmdb.m
//  zctx
//
//  Created by apple on 16/4/11.
//  Copyright © 2016年 sanliang. All rights reserved.
//

#import "ZCSearchFmdb.h"

@interface ZCSearchFmdb()

@property (nonatomic, strong) FMDatabase *fmdb;
@property (nonatomic, strong) FMDatabaseQueue *databaseQueue;

@end

#define SQL_TEXT_NAME @"Result.sql"
#define DATABASE_NAME @"searchResult.db"

@implementation ZCSearchFmdb

+ (ZCSearchFmdb *)sharedInstance
{
    static ZCSearchFmdb *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

- (instancetype)init
{
    self = [super init];
    if (self)
    {
    }
    return self;
}

//初始化
-(void)initSearchManager {
    if (![FileManager fileExistAtCacheDirectoryWithName:DATABASE_NAME])
    {
        
        self.databaseQueue = [FMDatabaseQueue databaseQueueWithPath:[FileManager cacheFilePathWithName:DATABASE_NAME]];
        
        NSString *sqlstr = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT, title text NOT NULL);"];
        
        [self.databaseQueue inDatabase:^(FMDatabase *db) {
            [db executeUpdate:sqlstr];
        }];
        
        NSLog(@"%@",[FileManager cacheFilePathWithName:DATABASE_NAME]);
    }
    else
    {
        self.databaseQueue = [FMDatabaseQueue databaseQueueWithPath:[FileManager cacheFilePathWithName:DATABASE_NAME]];
    }
}

// 插入数据
- (void)insertTable:(NSString *)str
{
    [self.databaseQueue inDatabase: ^(FMDatabase *db)
     {
         NSString *sql = [NSString stringWithFormat:@"INSERT INTO searchResult (title) VALUES '%@';",str];
         
         NSLog(@"sql =  %@",sql);
        [db executeUpdate:sql];         
     }];
}

// 公开方法: 获取所有的省
- (NSMutableArray *)getAllProvinces
{
    NSMutableArray *mArrayResults = [NSMutableArray new];
    [self.databaseQueue inDatabase: ^(FMDatabase *db)
     {
         NSString * sql = @"SELECT title FROM searchResult WHERE id <= 10 ORDER BY id";
         if ([db hadError]) {
             NSLog(@"DB Error %d: %@", [db lastErrorCode], [db lastErrorMessage]);
         }
         FMResultSet *rs = [db executeQuery:sql];
         while ([rs next])
         {
             NSLog(@"rs = %@",rs);
             DZRegionProvinceModel *model = [DZRegionProvinceModel new];
             model.code = [rs stringForColumnIndex:0];
             model.name = [rs stringForColumnIndex:1];
             [mArrayResults addObject:model];
         }
         [rs close];
     }];
    return mArrayResults;
}

// 公开方法: 根据省code获取相应所有的市
- (NSMutableArray *)getAllCitiesByProvinceCode:(NSString *)strCode
{
    
    NSMutableArray *mArrayResults = [NSMutableArray new];
    [self.databaseQueue inDatabase: ^(FMDatabase *db)
     {
         NSString * sql = [NSString stringWithFormat:@"SELECT code, name FROM regions WHERE code BETWEEN %@*100 AND %@*100+99 ORDER BY code", strCode, strCode];
         FMResultSet *rs = [db executeQuery:sql];
         while ([rs next])
         {
             DZRegionCityModel *model = [DZRegionCityModel new];
             model.code = [rs stringForColumnIndex:0];
             model.name = [rs stringForColumnIndex:1];
             [mArrayResults addObject:model];
         }
         [rs close];
     }];
    return mArrayResults;
}

@end
